package git.soulbgm.config;

import git.soulbgm.utils.IpUtil;
import git.soulbgm.utils.SpringBeanUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Map;

/**
 * 配置类
 *
 * @author 贺瑞杰
 * @version V1.0
 * @date 2018-05-29 18:02
 */
@Configuration
@ConfigurationProperties(prefix = "info", ignoreUnknownFields = false)
public class SysInfo {

    private Map<String, Integer> serviceCodeMap;

    private Map<String, Integer> nodeCodeMap;

    private Map<String, List<String>> deployNodeMap;

    @Value("${spring.application.name}")
    private String applicationName;

    /**
     * 服务标识
     */
    private Integer serviceCode;

    /**
     * 节点标识
     */
    private Integer nodeCode;

    private static SysInfo sysInfo;

    /**
     * 获取SysInfo
     *
     * @return {@link SysInfo}
     */
    public static SysInfo getConfig() {
        if (sysInfo == null) {
            sysInfo = SpringBeanUtil.getBean(SysInfo.class);
        }
        return sysInfo;
    }

    public Map<String, Integer> getServiceCodeMap() {
        return serviceCodeMap;
    }

    public void setServiceCodeMap(Map<String, Integer> serviceCodeMap) {
        this.serviceCodeMap = serviceCodeMap;
        Integer serviceCode = serviceCodeMap.get(applicationName);
        if (serviceCode == null) {
            serviceCode = 10;
        }
        setServiceCode(serviceCode);
    }

    public Map<String, Integer> getNodeCodeMap() {
        return nodeCodeMap;
    }

    public void setNodeCodeMap(Map<String, Integer> nodeCodeMap) {
        this.nodeCodeMap = nodeCodeMap;
        String host = IpUtil.getLocalHost();
        Integer nodeCode = nodeCodeMap.get(host);
        if (nodeCode == null) {
            nodeCode = 10;
        }
        setNodeCode(nodeCode);
    }

    public Map<String, List<String>> getDeployNodeMap() {
        return deployNodeMap;
    }

    public void setDeployNodeMap(Map<String, List<String>> deployNodeMap) {
        this.deployNodeMap = deployNodeMap;
    }

    public Integer getServiceCode() {
        return serviceCode;
    }

    public void setServiceCode(Integer serviceCode) {
        this.serviceCode = serviceCode;
    }

    public Integer getNodeCode() {
        return nodeCode;
    }

    public void setNodeCode(Integer nodeCode) {
        this.nodeCode = nodeCode;
    }

    public String getApplicationName() {
        return applicationName;
    }

    public void setApplicationName(String applicationName) {
        this.applicationName = applicationName;
    }
}
